﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Web.SessionState;
using Baimasoft.Manage.SqlServerDAL;

public partial class SysManage_MenuTree_left : System.Web.UI.Page
{
    public SqlConnection myconn;
    public SqlCommand mycomm;
    public SqlDataReader mydr;
    public SqlDataAdapter myadapter;
    public DataSet myds;
    public DataView mydv;
    public string sHtml = "";
    MenuManage MyMM = new MenuManage();
    protected void Page_Load(object sender, EventArgs e)
    {
        ShowTree();
    }

    /// <summary>
    /// 展现菜单
    /// </summary>
    public void ShowTree()
    {
        //获取上一次页面传递的信息
        string sSelectedCode = (string)Request.QueryString["selectedCode"];
        if (sSelectedCode == null)
            sSelectedCode = "";//当为空时,就说明是第一次打开此页面.展示树根

        //当为根目录且没有创建目录(页面)时


        string sRootLeaflessIconHtml = "<img height=\"20\" src=\"img/sys/root_leafless.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //有子目录(页面)且没有上下级时执行此语句
        string sRootRetractIconHtml = "<img height=\"20\" src=\"img/sys/root_retract.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //没有上下目录(页面)并且是已展开时,
        string sRootSpreadIconHtml = "<img height=\"20\" src=\"img/sys/root_spread.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //有上下目录且没有子目录(页面)时执行此语句
        string sMidLeaflessIconHtml = "<img height=\"20\" src=\"img/sys/mid_leafless.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //有上下目录且有子目录(页面)时执行此语句
        string sMidRetractIconHtml = "<img height=\"20\" src=\"img/sys/mid_retract.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //有上下目录且已展开下一级子目录(页面)时执行此语句
        string sMidSpreadIconHtml = "<img height=\"20\" src=\"img/sys/mid_spread.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //有上级目录且没有子目录时
        string sRearLeaflessIconHtml = "<img height=\"20\" src=\"img/sys/rear_leafless.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //有上级目录(页面)且没有展开子目录(页面)时


        string sRearRetractIconHtml = "<img height=\"20\" src=\"img/sys/rear_retract.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //有上级目录(页面)且已展开子目录(页面)时


        string sRearSpreadIconHtml = "<img height=\"20\" src=\"img/sys/rear_spread.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //文件夹合上时
        string sFolderCloseIconHtml = "<img height=\"20\" src=\"img/sys/folder_close.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //文件夹打开时


        string sFolderOpenIconHtml = "<img height=\"20\" src=\"img/sys/folder_open.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //没有选择页面时


        string sPageUnselectedIconHtml = "<img height=\"20\" src=\"img/sys/page_unselected.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //一条竖线


        string sVerticalIconHtml = "<img height=\"20\" src=\"img/sys/vertical.gif\" width=\"19\" align=\"absMiddle\" border=\"0\">";
        //背景
        string sBlankIconHtml = "<img src=\"img/sys/blank.gif\" align=\"absmiddle\" border=\"0\" WIDTH=\"19\" HEIGHT=\"20\">";
        try
        {
            //返回临时表中的信息
            SqlDataReader ShowTreeReader = MyMM.SpreadMenuTree(sSelectedCode); // mycomm.ExecuteReader();
            while (ShowTreeReader.Read())
            {
                //从数据库存储过程所创建的临时表中取数据

                //展开结点树时，缩进的结点前面是否有上(几)级结点的垂线
                string sIndentVertical = ShowTreeReader["IndentVertical"].ToString().Trim();
                //是否已经展开
                string isSpreaded = ShowTreeReader["Spreaded"].ToString().Trim();
                //是否同族的最后一个结点


                string isLastSibling = ShowTreeReader["LastSibling"].ToString().Trim();
                //是否有叶子结点


                string isLeafed = ShowTreeReader["Leafed"].ToString().Trim();
                // 结点编码
                string sCode = ShowTreeReader["Code"].ToString().Trim();
                //结点类型
                string cType = ShowTreeReader["Type"].ToString().Trim();
                //若是页面其对应的页面ID
                int iHomologID = (int)ShowTreeReader["Page_ID"];
                //结点标签
                string sLabel = ShowTreeReader["Label"].ToString().Trim();
                //结点的父结点编码
                string sParentCode = ShowTreeReader["ParentCode"].ToString().Trim();
                //若在同族中添加新结点，该新结点的编码
                string sNextCode = ShowTreeReader["NextCode"].ToString().Trim();
                //若在下一级中添加新结点，该新结点的编码


                string sNextSubCode = ShowTreeReader["NextSubCode"].ToString().Trim();

                /* 组合html语句 */
                //左边的+ - |的线条


                string sSpreadNodeLinkHtml_fore = "<a style=\"cursor:pointer\" onclick=\"initView();location.href='MenuTree_left.aspx?selectedCode=" + sCode + "';\">";
                string sRetractNodeLinkHtml_fore = "<a style=\"cursor:pointer\" onclick=\"initView();location.href='MenuTree_left.aspx?selectedCode=" + sCode.Substring(0, sCode.Length - 3) + "';\">";
                //通过sendCtrlNodeInfo赋值给Sys_Ctrl_right.aspx页面中的hidden.
                string sShowNodeInfoHtml = "<a style=\"cursor : pointer\" onclick=sendCtrlNodeInfo(\"" + sCode + "\",\"" + cType + "\",\"" + iHomologID + "\",\"" + sLabel + "\",\"" + sParentCode + "\",\"" + sNextCode + "\",\"" + sNextSubCode + "\",\"" + isLeafed + "\")><font size=\"-1\">&nbsp;" + sLabel + "</font></a>";

                //在左边写了一行信息


                if (sCode.Equals("Men"))
                { //根结点


                    String sRootHtml = "";
                    if (isLeafed.Equals("0")) //无子结点
                        sRootHtml = sRootLeaflessIconHtml + sFolderCloseIconHtml;
                    else
                    { //有子结点
                        if (sSelectedCode == null || sSelectedCode.Equals(""))
                        { //初始状态,连根结点都未被点击的情况 或 根结点收缩后的情况


                            sRootHtml = sSpreadNodeLinkHtml_fore + sRootRetractIconHtml + "</a>" + sFolderCloseIconHtml;
                        }
                        else
                        { //根结点已经展开的情况


                            sRootHtml = sRetractNodeLinkHtml_fore + sRootSpreadIconHtml + "</a>" + sFolderOpenIconHtml;
                        }
                    }
                    sRootHtml = sRootHtml + sShowNodeInfoHtml;
                    sHtml = sHtml + "<div>" + sRootHtml + "</div>";
                }
                else
                { //非根结点
                    String sNodeHtml = "";
                    //处理缩进
                    String sIndentHtml = "";
                    int i = 0;
                    char cIsVertical;
                    //sIndentVertical表示有几级结点


                    while (i < sIndentVertical.Length)
                    {
                        cIsVertical = sIndentVertical[i];
                        if (cIsVertical == '0')
                            sIndentHtml = sIndentHtml + sBlankIconHtml;
                        else
                            sIndentHtml = sIndentHtml + sVerticalIconHtml;
                        i = i + 1;
                    }

                    if (cType.Equals("1"))//是目录时
                    { //1.树枝结点
                        if (isLastSibling.Equals("0"))
                        { //2.非尾结点
                            if (isLeafed.Equals("1"))
                            { //3.有子结点
                                if (isSpreaded.Equals("0"))
                                { //4.未展开
                                    sNodeHtml = sSpreadNodeLinkHtml_fore + sMidRetractIconHtml + "</a>" + sFolderCloseIconHtml;
                                }
                                else
                                { //4.已展开
                                    sNodeHtml = sRetractNodeLinkHtml_fore + sMidSpreadIconHtml + "</a>" + sFolderOpenIconHtml;
                                }
                            }
                            else
                            { //3.无子结点
                                sNodeHtml = sMidLeaflessIconHtml + sFolderCloseIconHtml;
                            }
                        }
                        else
                        { //2.尾结点


                            if (isLeafed.Equals("1"))
                            { //3.有子结点
                                if (isSpreaded.Equals("0"))
                                { //4.未展开
                                    sNodeHtml = sSpreadNodeLinkHtml_fore + sRearRetractIconHtml + "</a>" + sFolderCloseIconHtml;
                                }
                                else
                                { //4.已展开
                                    sNodeHtml = sRetractNodeLinkHtml_fore + sRearSpreadIconHtml + "</a>" + sFolderOpenIconHtml;
                                }
                            }
                            else
                            { //3.无子结点
                                sNodeHtml = sRearLeaflessIconHtml + sFolderCloseIconHtml;
                            }
                        }
                    }
                    else if (cType.Equals("0")) //是页面时
                    { //1.树叶结点
                        if (isLastSibling.Equals("0"))  //2.非尾结点
                            sNodeHtml = sMidLeaflessIconHtml + sPageUnselectedIconHtml;
                        else  //2.尾结点


                            sNodeHtml = sRearLeaflessIconHtml + sPageUnselectedIconHtml;
                    }
                    sNodeHtml = sIndentHtml + sNodeHtml + sShowNodeInfoHtml;
                    sHtml = sHtml + "<div>" + sNodeHtml + "</div>";
                }
            }
        }
        catch
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请刷新页面!');</script>");
        }
    }

    /// <summary>
    ///  发布
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SubmitTree(object sender, ImageClickEventArgs e)
    {
        int back = MyMM.CreateMenuTree("0").Rows.Count;
        if (back > 0)
        {
            //ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('发布成功');var i=screen.availWidth;var j=screen.availHeight;javascript:window.open('index.htm','白马软件后台管理系统','toolbar=no,left=0,top=0,titlebar=no,location=no,status=yes,menubar=no,scrollbars=no,resizable=yes,scrolling=no,width='+i+',height='+j+'');window.opener=null;window.close();<" + "/script>");
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script language=javascript>alert('发布成功'); window.parent.parent.leftFrame.location.reload();<" + "/script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('发布失败!');</script>");
        }
    }
}